<?php

/**
 * 合同负责人
 * @author liuyw01 2016-02-16
 */

namespace common\repositories\myhtsq;

use common\repositories\BaseRepository;
use common\entities\myhtsq\ContractPrincipalEntity;
use common\error\ErrorCode;
use common\repositories\common\MyscrmContractSyncDeleteDataRepository;

class ContractPrincipalRepository extends BaseRepository
{

    /**
     * 构造方法
     * @param unknown $config (optional)
     */
    public function __construct($config = [])
    {
        parent::__construct($config);
    }

    /**
     * 文档列表
     * @param string $contractId 合同id
     * @return array
     */
    public static function getPrincipalListByContractId($contractId)
    {
        return ContractPrincipalEntity::find()
            ->where(['contract_id' => $contractId])
            ->asArray()
            ->all();
    }

    /**
     * 新增主责人
     * @param $principalEntity
     * @return boolean|string 失败返回false，成功返回新增的文档Id
     */
    public static function addPrincipal($principalEntity)
    {
        $entity = new ContractPrincipalEntity ();
        $data = $principalEntity->attributes;
        $entity->load($data, '');
        if ($entity->save()) {
            return $entity->principal_id;
        } else {
            return false;
        }
    }

    /**
     * 根据文件id,删除文件记录
     * @param string $principal_id 文件id
     * @return boolean
     */
    public static function delPrincipalById($principal_id)
    {
        /** @var MyscrmContractSyncDeleteDataRepository $contractSyncDeleteDataRepository */
        $contractSyncDeleteDataRepository = \Yii::createObject(MyscrmContractSyncDeleteDataRepository::className());

        $deleteResult = ContractPrincipalEntity::deleteAll(["principal_id" => $principal_id]);

        if ($deleteResult > 0) {
            $contractSyncDeleteDataRepository->add('myscrm_contract_principal', 'principal_id', $principal_id);
        }

        return $deleteResult;
    }

    /**
     * 更新
     * @param $principalModel 签约负责人模型
     * @return string|boolean
     */
    public static function updatePrincipal($principalModel)
    {
        $entity = new ContractPrincipalEntity ();
        $modelInfo = ContractPrincipalEntity::find()->where(['principal_id' => $principalModel->principal_id])->one();
        $data = $principalModel->attributes;
        $modelInfo->load($data, '');
        if ($modelInfo->save()) {
            return $entity->principal_id;
        } else {
            return false;
        }
    }

    /**
     * 根据合同id删除合同负责人
     * @param $contractId
     * @return int
     */
    public static function deleteByContractId($contractId)
    {
        /** @var MyscrmContractSyncDeleteDataRepository $contractSyncDeleteDataRepository */
        $contractSyncDeleteDataRepository = \Yii::createObject(MyscrmContractSyncDeleteDataRepository::className());

        $deleteData = ContractPrincipalEntity::findAll(["contract_id" => $contractId]);

        $deleteResult = ContractPrincipalEntity::deleteAll(["contract_id" => $contractId]);

        if($deleteResult > 0){
            foreach ($deleteData as $data){
                $contractSyncDeleteDataRepository->add('myscrm_contract_principal', 'principal_id', $data['principal_id']);
            }
        }

        return $deleteResult;
    }

}
